package firmware

import (
	"context"
	v1 "devops-super/api/product/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.GetFirmwarePageLstReq) (res *v1.GetFirmwarePageLstRes, err error) {
	res = &v1.GetFirmwarePageLstRes{}
	res.PageLstRes, err = service.Firmware().GetPageLst(ctx, req)
	return
}

func (*ControllerV1) Add(ctx context.Context, req *v1.AddFirmwareReq) (res *v1.AddFirmwareRes, err error) {
	firmware := &entity.AppFirmware{
		Name:      req.Name,
		ProductId: req.ProductId,
		Version:   req.Version,
		Part:      req.Part,
		Remark:    req.Remark,
		FileMd5:   req.FileMd5,
		FileUrl:   req.FileUrl,
		WhereAt:   req.WhereAt,
		AppFileId: req.AppFileId,
		Image:     req.Image,
		CreatedAt: gtime.Now(),
	}
	err = service.Firmware().Add(ctx, firmware)
	return
}

func (*ControllerV1) Upt(ctx context.Context, req *v1.UptFirmwareReq) (res *v1.UptFirmwareRes, err error) {
	firmware := &entity.AppFirmware{
		Id:        uint64(req.Id),
		Name:      req.Name,
		ProductId: req.ProductId,
		Version:   req.Version,
		Part:      req.Part,
		Remark:    req.Remark,
		FileMd5:   req.FileMd5,
		FileUrl:   req.FileUrl,
		WhereAt:   req.WhereAt,
		AppFileId: req.AppFileId,
		Image:     req.Image,
		UpdatedAt: gtime.Now(),
	}
	err = service.Firmware().Upt(ctx, firmware)
	return
}

func (*ControllerV1) Del(ctx context.Context, req *v1.DelFirmwareReq) (res *v1.DelFirmwareRes, err error) {
	err = service.Firmware().Del(ctx, req.Id)
	return
}

func (*ControllerV1) GetOne(ctx context.Context, req *v1.GetFirmwareOneReq) (res *v1.GetFirmwareOneRes, err error) {
	res1, err := service.Firmware().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.GetFirmwareOneRes)
	res.Info = res1
	return
}

func (*ControllerV1) GetOptions(ctx context.Context, req *v1.GetFirmwareOptionsReq) (res *v1.GetFirmwareOptionsRes, err error) {
	res, err = service.Firmware().GetOptions(ctx, req)
	return
}
